package com.jiaai.cloud.active.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.active.entity.TActivity;
import com.jiaai.cloud.active.entity.TActivitySearch;
import com.jiaai.cloud.active.service.ITActivityService;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.gift.entity.TLiveGift;
import com.jiaai.cloud.system.entity.TLiveServerSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 活动 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-09-01
 */
@RestController
@RequestMapping("/api/manage/tActivity")
public class TActivityController {
    @Autowired
    private ITActivityService  itActivityService;

    //查所有带分页
    @RequestMapping(value ="/findByAll",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findByAll(FrontPage page, Integer platform) throws ServiceException {
        try {
//            Page page =new Page();
            EntityWrapper<TActivity> wrapper=new EntityWrapper<>();
            if(platform==null){
                return RestResultGenerator.genSuccessResult(itActivityService.selectPage(page.getPagePlus(),wrapper));
            }
            wrapper.eq("platform", platform);
            return RestResultGenerator.genSuccessResult(itActivityService.selectPage(page.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //    删除
    @RequestMapping(value = "/deleteId/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itActivityService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改,保存(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TActivity tActivity)throws ServiceException{
        try {
            tActivity.setImg("http://bj.huodongwang.com/data/attachment/cnyinglan_huodongs/2015/0619/103638ao9aai9rsaa8uhr3.jpg");
            tActivity.setStartTime(new Date());
            tActivity.setEndTime(new Date());
            itActivityService.insertOrUpdate(tActivity);
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }
	
}
